package com.software.loginregister.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.software.loginregister.R;
import com.software.loginregister.entity.vo.WorksListVO;

import java.util.ArrayList;
import java.util.List;

public class FollowPostAdapter extends RecyclerView.Adapter<FollowPostAdapter.ViewHolder> {

    private List<WorksListVO> posts = new ArrayList<>();
    private Context context;

    public FollowPostAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_follow_post, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        WorksListVO post = posts.get(position);
        holder.tvFollowPostTitle.setText(post.getWorksName());
        holder.tvFollowUsername.setText(post.getNickname());
        holder.tvFollowPostContent.setText(post.getDescription());
        holder.tvFollowPostLikeCount.setText(String.valueOf(post.getLikeCount()));

        // 设置难度和食材
        String difficultyAndIngredients = "难度: " + post.getDifficulty() + " | 食材: " + post.getIngredients();
        holder.tvFollowPostContent.setText(difficultyAndIngredients + "\n" + post.getDescription());

        Glide.with(context).load(post.getAvatar()).placeholder(R.drawable.ic_default_avatar).into(holder.ivFollowUserAvatar);
        Glide.with(context).load(post.getPhotoUrl()).into(holder.ivFollowPostImage);

        // 由于布局中没有评论数，我们可以暂时隐藏这个视图
        holder.tvFollowPostCommentCount.setVisibility(View.GONE);
    }

    @Override
    public int getItemCount() {
        return posts.size();
    }

    public void setData(List<WorksListVO> newPosts) {
        posts.clear();
        posts.addAll(newPosts);
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvFollowPostTitle;
        ImageView ivFollowUserAvatar;
        TextView tvFollowUsername;
        TextView tvFollowPostContent;
        ImageView ivFollowPostImage;
        TextView tvFollowPostLikeCount;
        TextView tvFollowPostCommentCount;

        ViewHolder(View itemView) {
            super(itemView);
            tvFollowPostTitle = itemView.findViewById(R.id.tv_follow_post_title);
            ivFollowUserAvatar = itemView.findViewById(R.id.iv_follow_user_avatar);
            tvFollowUsername = itemView.findViewById(R.id.tv_follow_username);
            tvFollowPostContent = itemView.findViewById(R.id.tv_follow_post_content);
            ivFollowPostImage = itemView.findViewById(R.id.iv_follow_post_image);
            tvFollowPostLikeCount = itemView.findViewById(R.id.tv_follow_post_like_count);
            tvFollowPostCommentCount = itemView.findViewById(R.id.tv_follow_post_comment_count);
        }
    }
}